﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Access.PowerTools.AbstractionLayer;
using SMSConsulting.SMSLIB.Utilities;

namespace Access.PowerTools.Simulation.Engine
{
    public class SimulationCommunicationBridge : IHostComunicationBridge 
    {
        bool IHostComunicationBridge.StartHostApplication() { return false; }
        bool IHostComunicationBridge.QuitHostApplication() { return false; }
        bool IHostComunicationBridge.OpenCurrentDatabase(string databaseFullPath) { return false; }
        bool IHostComunicationBridge.CloseCurrentDatabase() { return false; }
        bool IHostComunicationBridge.CompactCurrentDatabase() { return false; }

        #region IHostComunicationBridge Members
        string IHostComunicationBridge.ApplicationName
        {
            get { return "Access.Application.Simulator"; }   
        }

        string IHostComunicationBridge.ApplicationVersion 
        {
            get { return "2000-2010"; }   
        }

        private IHostComunicationBridge iHostBridge { get { return this; } }

        private AccessDatabase _database;

        AccessDatabase IHostComunicationBridge.CurrentDatabase 
        {
            get
            {
                if (_database == null) initDatabase();
                return _database; 
            }
        }

        string IHostComunicationBridge.CurrentDatabaseFullPath 
        {
            get
            {
                return null; // not used here
            }
        }

        private List<AccessDatabaseObjectInfo> _objects;
        private void initDatabase()
        {
            _objects = new List<AccessDatabaseObjectInfo>(); 
            
            string seedFolder =
                 Access.PowerTools.Settings.Globals.NorthdwindDatabaseSeedFilesFolder;
            string[] files = System.IO.Directory.GetFiles(seedFolder);

            foreach (string file in files)
            {
                string fileName = System.IO.Path.GetFileName(file);

                if (fileName.EndsWith("Schema.xml")) continue;
                if (fileName.EndsWith("_TextData.txt")) continue;

                AccessDataBaseObjectTypeEnumeration type =
                    AccessDatabaseObjectInfo.GetObjectTypeByFileName(fileName);
                string objectName = AccessDatabaseObjectInfo.GetObjectNameFromFileName(fileName);

                AccessDatabaseObjectInfo accessObject = new AccessDatabaseObjectInfo(type, objectName);

                _objects.Add(accessObject);
            }

            _database = new AccessDatabase(this);

        }

        List<AccessDatabaseObjectInfo> IHostComunicationBridge.GetObjectsList()
        {
            if (_database == null) initDatabase();
            return _objects;
        }


        bool IHostComunicationBridge.ObjectExists(AccessDataBaseObjectTypeEnumeration objectType, string objectName)
        {
            AccessDatabase database = this.iHostBridge.CurrentDatabase;
            if (database == null) return false;

            AccessDatabaseObjectInfo objectInfo = database.GetObjectInfo(objectType, objectName);    

            return (objectInfo != null);
        }

        void IHostComunicationBridge.SaveAsText(AccessDataBaseObjectTypeEnumeration objectType, string objectName, string backUpFileFullPath)
        {
            AccessDatabase database = this.iHostBridge.CurrentDatabase;
            AccessDatabaseObjectInfo objectInfo =
                database.GetObjectInfo(objectType, objectName);
            RunTimeAssert.NotNull(objectInfo,
                string.Format("Can't find object to backup {0} => {1}", objectType, objectName));

            System.IO.File.Copy(objectInfo.SimulationSeedObjectInfoFileFullPath, backUpFileFullPath, true);

            if (objectType == AccessDataBaseObjectTypeEnumeration.Table)
            {
                string sourceFolder = System.IO.Path.GetDirectoryName(objectInfo.SimulationSeedObjectInfoFileFullPath) +
                                      System.IO.Path.DirectorySeparatorChar;
                string backUpFolder = System.IO.Path.GetDirectoryName(backUpFileFullPath) +
                                      System.IO.Path.DirectorySeparatorChar;

                // Schema.xml
                string fileName = AccessDatabaseObjectInfo.GetBackUpFileNameForObjectSchema(objectType, objectName);

                System.IO.File.Copy(sourceFolder + fileName,
                                    backUpFolder + fileName,
                                    true);

                // _TextData.txt
                fileName = AccessDatabaseObjectInfo.GetBackUpFileNameForDelimitedTextExport(objectType, objectName);

                System.IO.File.Copy(sourceFolder + fileName,
                                    backUpFolder + fileName,
                                    true);

            }
        }

        void IHostComunicationBridge.LoadFromText(AccessDataBaseObjectTypeEnumeration objectType, string objectName, string backUpFileFullPath)
        {
            string restoreFolderForSimulation = Access.PowerTools.Settings.Globals.RestoredFilesFolder;

            //Console.WriteLine("Restore: {0}.{1} from {2} = {3}",
            //    objectType, objectName, backUpFileFullPath, System.IO.File.Exists(backUpFileFullPath));  

            AccessDatabaseObjectInfo objectInfo =
                this.iHostBridge.CurrentDatabase.GetObjectInfo(objectType, objectName);

            if (objectInfo == null)
            {
                objectInfo = new AccessDatabaseObjectInfo(objectType, objectName);   
            }

            string restoreFileFullPath = System.IO.Path.Combine(
                restoreFolderForSimulation,
                objectInfo.BackUpFileName); 
                //System.IO.Path.GetFileName(backUpFileFullPath));

            System.IO.File.Copy(backUpFileFullPath, restoreFileFullPath, true);

            if (objectType == AccessDataBaseObjectTypeEnumeration.Table)
            {
                string backUpFolder = System.IO.Path.GetDirectoryName(backUpFileFullPath);
                string restoreFolder = System.IO.Path.GetDirectoryName(restoreFileFullPath);

                //  Schema.xml
                string fileName = AccessDatabaseObjectInfo.GetBackUpFileNameForObjectSchema(objectType, objectName);

                System.IO.File.Copy(
                            System.IO.Path.Combine(backUpFolder, fileName),
                            System.IO.Path.Combine(restoreFolder, fileName),
                                    true);
                // _TextData.txt
                fileName = AccessDatabaseObjectInfo.GetBackUpFileNameForDelimitedTextExport(objectType, objectName);

                System.IO.File.Copy(
                            System.IO.Path.Combine(backUpFolder, fileName),
                            System.IO.Path.Combine(restoreFolder, fileName),
                                    true);
            }
        }

        #endregion
    }
}
